package com.mxin.image.glide;

import android.content.Context;

import com.bumptech.glide.Glide;
import com.bumptech.glide.GlideBuilder;
import com.bumptech.glide.Registry;
import com.bumptech.glide.load.engine.cache.DiskLruCacheFactory;
import com.bumptech.glide.load.engine.cache.LruResourceCache;
import com.bumptech.glide.load.model.GlideUrl;
import com.mxin.util.FileUtil;

import java.io.InputStream;

import androidx.annotation.NonNull;

/**
 * Created by ：2020/8/4
 * author : Administrator
 */
public class DefaultGlideModelImpl implements IGlideModelListener {


    @Override
    public void registerComponents(@NonNull Context context, @NonNull Glide glide, @NonNull Registry registry) {
        registry.replace(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory());
    }

    @Override
    public void applyOptions(@NonNull Context context, @NonNull GlideBuilder builder) {
        //默认内存的缓存大小
        long defaultMemoryCacheSize = 8 * 1024 * 1024L;
        //运行内存的缓存大小
        long runtimeMemoryCacheSize = Runtime.getRuntime().maxMemory() / 8;
        //默认本地的缓存大小
        long defaultDiskCacheSize = 250 * 1024 * 1024L;
        if (runtimeMemoryCacheSize > defaultDiskCacheSize) {
            defaultMemoryCacheSize = runtimeMemoryCacheSize;
        }
        builder.setMemoryCache(new LruResourceCache(defaultMemoryCacheSize))
                .setDiskCache(new DiskLruCacheFactory(FileUtil.getPrivatePicturePath(), defaultDiskCacheSize));
    }

}
